var WxParse = require('../../wxParse/wxParse.js');
//获取应用实例
var app = getApp()
Page({
  data: {
    curId: '',
    payWay:1
  },
  onLoad: function (e) {
    var that = this;
    app.setNavtitle("店铺续费");
    if(e&&e.id){
      that.setData({
        id:e.id
      })
    }
    that.requestSettledTime();
    that.requestpayType();
  },
  requestSettledTime: function () {
    var that = this;
    wx.showLoading({
      title: '正在加载',
      mask: true,
      time: 100000
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_mobile_shop_apply_cost'
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          that.setData({
            settledTime: res.data.data,
            curId: res.data.data ? res.data.data[0].id:'',
            payMoney: res.data.data ? res.data.data[0].amount:''
          })
        } else {
          console.log(res.data)
          that.setData({
            settledTime: []
          })
          // app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  requestpayType: function () {
    var that = this;
    wx.showLoading({
      title: '正在加载',
      mask: true,
      time: 100000
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_pay_cfg'
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          that.setData({
            payType: res.data.data,
            payWay: res.data.data ? res.data.data[0].type : 1
          })
        } else {
          console.log(res.data)
          that.setData({
            payType: []
          })
          // app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  chooseSettledtime:function(e){
    var that = this;
    var type = e.currentTarget.dataset.type;
    var payMoney = e.currentTarget.dataset.money;
    that.setData({
      curId: type,
      payMoney: payMoney
    })
  },
  nowPay: function () {
    var that = this;
    var data = {
      map: 'applet_mobile_shop_pay',
      dateId: that.data.curId,
      payType: that.data.payWay
    }
    if (!data.dateId) {
      app.errorTip(that, "请选择入驻时长", 2000);
      return;
    }
    console.log(data);
    wx.showLoading({
      title: '加载中',
      time: 100000,
      mask: true
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      dataType: 'json',
      success: function (res) {
        console.log(res);
        if (res.data.ec == 200) {
          var orderdata = res.data.data;
          console.log("支付方式" + data.payType)
          if (data.payType == 1) {
            console.log("微信支付");
            that.postPay(orderdata.params, orderdata.number);
          } else {
            console.log("余额支付");
            that.requestRenew(orderdata.number);
          }
        } else {
          console.log(res.data)
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  requestRenew:function(number){
    var that = this;
    var data = {
      map: 'applet_mobile_shop_renew',
      amsId: that.data.id,
      number: number
    }
    console.log(data);
    wx.showLoading({
      title: '正在加载',
      mask: true,
      time: 100000
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          wx.showModal({
            title: '提示',
            content: res.data.data.msg,
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        } else {
          console.log(res.data)
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  postPay: function (params, tid) {
    var that = this;
    wx.requestPayment({
      'appId': params.appId,
      'timeStamp': params.timeStamp,
      'nonceStr': params.nonceStr,
      'package': params.package,
      'signType': params.signType,
      'paySign': params.paySign,
      'success': function (res) {
        wx.showLoading({
          title: '加载中',
        })
        setTimeout(function () {
          wx.hideLoading();
          that.requestRenew(tid)
        }, 1000)
      },
      'fail': function (res) {
        console.log(res);
        wx.showModal({
          title: '',
          content: '支付失败',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {

            }
          }
        })
      }
    });
  },
  payRadioChange: function (e) {
    this.setData({
      payWay: e.detail.value
    })
    console.log(this.data.payWay);
  },
})
